Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve metadata tutorial #12931

Merged
merged 16 commits into from
Nov 7, 2024
Merged

Conversation

cbrnr
Copy link
Contributor

@cbrnr cbrnr commented Oct 31, 2024

I might have found some things that could be improved in the excellent metadata tutorial:

  • The text did not match what the code was doing in the stimulus-locked example (all correct epochs vs. slow correct epochs).
  • I am not sure what the "final sanity check" is supposed to show - the output only shows an empty table, but the text says "Bummer! It seems the very first two responses were recorded before the first stimulus appeared: the values in the stimulus column are None." I don't really see this in the output?
  • Maybe this is related to 2, but the ERN example (response-locked) shows a metadata table with 402 rows, whereas the stimulus-locked metadata has only 400 rows. This should not be the case, right?
  • The second-to-last plot shows two images side by side. First, I don't know how these were created (the code shows two independent plot commands). Second, the title of the right plot is cut off.
  • Mention actual authors at the top.

Closes #12939

@hoechenberger
Copy link
Member

Thanks @cbrnr, I might be able to take a look later today or tomorrow (I think I initially wrote a larger part of this very tutorial)

@hoechenberger
Copy link
Member

  • I am not sure what the "final sanity check" is supposed to show - the output only shows an empty table, but the text says "Bummer! It seems the very first two responses were recorded before the first stimulus appeared: the values in the stimulus column are None." I don't really see this in the output?

I checked and it shows the correct output in the rendered documentation up until MNE-Python 1.6:

https://mne.tools/1.6/auto_tutorials/epochs/40_autogenerate_metadata.html#applying-the-knowledge-visualizing-the-ern-component

For newer versions, it generates an empty table, like you observed. I'll try to look into this.

@hoechenberger
Copy link
Member

hoechenberger commented Nov 3, 2024

  • Maybe this is related to 2, but the ERN example (response-locked) shows a metadata table with 402 rows, whereas the stimulus-locked metadata has only 400 rows. This should not be the case, right?

Yes this is to be expected, there were 400 stimuli but 402 button presses. In our analysis, we wish to only consider the first button press following a stimulus.

This is directly related to your previous question / observation.

@hoechenberger
Copy link
Member

hoechenberger commented Nov 3, 2024

Ok I found the problem.

MWE:

# %%
import mne

data_dir = mne.datasets.erp_core.data_path()
infile = data_dir / "ERP-CORE_Subject-001_Task-Flankers_eeg.fif"

raw = mne.io.read_raw(infile, preload=True)
raw.filter(l_freq=0.1, h_freq=40)
all_events, all_event_id = mne.events_from_annotations(raw)

metadata_tmin, metadata_tmax = -1.5, 0
row_events = ["response/left", "response/right"]
keep_last = ["stimulus", "response"]

metadata, events, event_id = mne.epochs.make_metadata(
    events=all_events,
    event_id=all_event_id,
    tmin=metadata_tmin,
    tmax=metadata_tmax,
    sfreq=raw.info["sfreq"],
    row_events=row_events,
    keep_last=keep_last,
)

# %%
metadata.loc[metadata["last_stimulus"] == "", :]

The cells with missing values are populated with empty strings. But there should be n/a in these instead.

@larsoner Any idea about this? It worked back with MNE-Python 1.6, and was broken in our rendered docs starting with 1.7. I'm not sure if it's anything we have changed, or if it's related to Pandas.

@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 4, 2024

Also, didn't we agree to keep the actual authors in all tutorials and examples? Currently, this document just contains

# Authors: The MNE-Python contributors.

@hoechenberger
Copy link
Member

I just tried with MNE 1.6 and I get the expected result. Switching to main while keeping all other installed packages the same yields the issue observed above. So it's likely a bug in MNE.

@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 4, 2024

Just to clarify the MWE, the output works for current versions (after the change), because it searches for metadata["last_stimulus"] == "", whereas MNE does metadata["last_stimulus"].isna().

@larsoner
Copy link
Member

larsoner commented Nov 4, 2024

Also, didn't we agree to keep the actual authors in all tutorials and examples? Currently, this document just contains

I think we decided that people can opt-in to adding their names in tutorials and examples if they are so inclined. Sounds like the author(s) of that tutorial haven't done that yet!

@hoechenberger hoechenberger marked this pull request as draft November 4, 2024 21:41
doc/changes/devel/12931.bugfix.rst Outdated Show resolved Hide resolved
ax[0].xaxis.set_visible(False)
ax[1].xaxis.set_visible(False)

fig
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not show the figure using plt.show()? Depending on where people execute this code, just typing fig might not actually show the figure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose @drammock or @larsoner would know an answer to this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But note that the tutorials are meant for interactive use and grouped into IPython cells, so …

Copy link
Contributor Author

@cbrnr cbrnr Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this wouldn't work in the standard Python interactive interpreter (unless interactive Matplotlib mode is enabled), it only works in a notebook. I was just wondering if plt.show() would be the canonical way to show a figure in both a regular interpreter and a notebook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever our expectation is, maybe worth clarifying that explicitly here? https://mne.tools/stable/auto_tutorials/index.html

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use plt.show() in sphinx-gallery generated tutorials.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with either as long as it works. can you make a suggestion or push a change?

Copy link
Contributor Author

@cbrnr cbrnr Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be clear, you are also relying on a notebook when you do

metadata

This will output an HTML table, so we cannot replace it with print(metadata) (and certainly we don't want to replace it with IPython.display.display()). So it's probably OK to assume we're in a notebook I guess (but we should clarify this on the tutorials and examples main pages)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use plt.show() in sphinx-gallery generated tutorials.

I think this is what we've done elsewhere, or just remove the fig line

tutorials/epochs/40_autogenerate_metadata.py Outdated Show resolved Hide resolved
tutorials/epochs/40_autogenerate_metadata.py Outdated Show resolved Hide resolved
mne/epochs.py Show resolved Hide resolved
@hoechenberger
Copy link
Member

@cbrnr You might be interested in this bug fix: 441c32e

@hoechenberger hoechenberger marked this pull request as ready for review November 5, 2024 13:07
@hoechenberger
Copy link
Member

I think this is ready for a final review

I'll be withdrawing from this PR as I'm super busy with other stuff
For additional changes, please just push them yourselves 🙏

@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 5, 2024

@cbrnr You might be interested in this bug fix: 441c32e

I'm not sure I understand what you mean...

@hoechenberger
Copy link
Member

hoechenberger commented Nov 5, 2024

@cbrnr You might be interested in this bug fix: 441c32e

I'm not sure I understand what you mean...

The bug was introduced because the current behavior of the average parameter in the plotting functions is difficult to understand / use correctly, as you brought up in #12857

@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 5, 2024

OK, this looks very good! One last (minor) gripe: we're using a line length of 88, which unfortunately is not rendered without a horizontal scrollbar (see the very last code block at the bottom of the document here). I opened #12945 for that so this can be merged!

ax[0].xaxis.set_visible(False)
ax[1].xaxis.set_visible(False)

fig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use plt.show() in sphinx-gallery generated tutorials.

I think this is what we've done elsewhere, or just remove the fig line

tutorials/epochs/40_autogenerate_metadata.py Show resolved Hide resolved
@cbrnr cbrnr force-pushed the fix-metadata-tutorial branch from fd8d376 to e2cbabb Compare November 7, 2024 09:35
@cbrnr
Copy link
Contributor Author

cbrnr commented Nov 7, 2024

This is now finally ready for review and merge! Thanks @hoechenberger for addressing my comments!

@hoechenberger hoechenberger enabled auto-merge (squash) November 7, 2024 13:21
@larsoner
Copy link
Member

larsoner commented Nov 7, 2024

Failures are just from scikit-learn/scikit-learn#30237 so in it goes, thanks @cbrnr !

@larsoner larsoner disabled auto-merge November 7, 2024 19:26
@larsoner larsoner merged commit 4608995 into mne-tools:main Nov 7, 2024
25 of 28 checks passed
@cbrnr cbrnr deleted the fix-metadata-tutorial branch November 11, 2024 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression in epochs metadata generation via change introduced in #12347
4 participants